$(document).ready(function(){
	initNote("#number1");
	function initNote(id){
		/* 即时贴可缩放 */
        $(id).resizable(
            {minHeight:150,minWidth:150,maxWidth:500,maxHeight:500},
            {/* 缩放结束后，同步即时贴尺寸 */
                stop:function(){
                    var width = parseInt($(this).css("width"));
                    var height = parseInt($(this).css("height"));
                    var headImg =$(this).find(".t");
                    var title_left = parseInt(parseInt(width-parseInt(headImg.css("width")))/2-20);
                    headImg.css("left", title_left+"px");
                    $(this).find(".ncontent").attr("style", "width:"+width+"px;height:"+height+";");
                    //修改左侧参数
					$("input[name='width']").val(width);
					$("input[name='height']").val(height);
					$("input[name='title_left']").val(title_left);
                }
            }
        );
		 /*鼠标悬停两秒后触发显示工具栏事件，鼠标经过不触发*/
        $(id).hover(function(){
        	time=setTimeout(function(){
        		showTools(id);
        	},400);
        },function(){
        	if(time){
        		clearTimeout(time);
        	}
        	hideTools(id);
        });
		/*点击换肤按钮*/
        $(id+" .pickcolor").on('click', function(){
        		$(id+" .colors").show();
        });
		 /*换肤*/
        $(id+" .colors span").on('click', function(){
        	var color=$(this).attr("class");
			$(id).removeClass($(id).attr("color")).addClass(color).attr("color",color);
			$("input[name='color'][value='"+color+"']").prop("checked", "true");
        	$(this).parent().hide();
        });
		/*换按钉*/
		$(id+" .t").on('click', function(){
			var style = $(id).attr("title_type");
			var num = style.subString(5, 6);
			alert(num);
			var newstyle = "style-"+(num+1);
			$(id).removeClass(style).addClass(newstyle).attr("title_type", newstyle);
		});
	}
function showTools(id){
	$(id+" .nadd").css("visibility","visible");
	$(id+" .nclose").css("visibility","visible");
	$(id+" .pickcolor").css("visibility","visible");
}
function hideTools(id){
		$(id+" .nadd").css("visibility","hidden");
		$(id+" .nclose").css("visibility","hidden");
		$(id+" .pickcolor").css("visibility","hidden");
}
$("input[name='color']").click(function(){
	var color = $(this).val();
	var note_example = $(".note_example");
	note_example.addClass(color).removeClass(note_example.attr("color")).attr("color", color);
});
$("input[name='style_t']").click(function(){
	var note_example = $(".note_example");
	note_example.removeClass(note_example.attr("titletype")).addClass("style-"+$(this).val()).attr("titletype", "style-"+$(this).val());
});
$("textarea[name='content']").keyup(function(){
	$(".note_example .ncontent").html($(this).val());
});
$("input[name='width']").keyup(function(){
	var width = parseInt($(this).val());
	if(width>500){
		width = 500;
	}
	if(width<150){
		width = 150;
	}
	$(".note_example").css("width", width);
	var t_left = (width - parseInt($("#number1 .t").css("width")))/2-20;
	$("input[name='title_left']").val(t_left);
	$("#number1 .t").css("left", t_left+"px");
});
$("input[name='height']").keyup(function(){
	var height = parseInt($(this).val());
	if(height>500){
		height = 500;
	}
	if(height<150){
		height = 150;
	}
	$(".note_example").css("height", height);
});
});
//表单提交
$(document).ready(function(){
	var options = {
		url: "/notetemplate/resetStickySetting",
		type: "post",
		beforeSubmit:function(formData, jqForm, options){
			var height = $("input[name=height]").val();
			var width = $("input[name=width]").val();
			var tips = $("#tips");
			tips.html("");
			tips.show();
			if(height == "" || width == ""){
				tips.html("高度和宽度都不能为空");
				return false;
			}
			return true;
		},
		success: function(data){
			var tips = $("#tips");
			if(data=="success"){
				tips.html("修改成功！");
			}else{
				tips.html("修改失败！");
			}
			tips.show();
			setTimeout("$('#tips').fadeOut('slow')", 2000);
		}
	};
    $('#form_sticky_setting').ajaxForm(options); 
});